home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / NDIROS2.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  7KB  |  204 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    Changes Copyright (c) 1989-1993 by Kendra Electronic            */
  3. /*    Wonderworks.                                                    */
  4. /*                                                                    */
  5. /*    All rights reserved except those explicitly granted by the      */
  6. /*    UUPC/extended license agreement.                                */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*                          RCS Information                           */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. /*
  14.  *    $Id: NDIROS2.C 1.4 1993/04/05 12:26:01 ahd Exp $
  15.  *
  16.  *    Revision history:
  17.  *    $Log: NDIROS2.C $
  18.  *     Revision 1.4  1993/04/05  12:26:01  ahd
  19.  *     Drop RCSID
  20.  *
  21.  *     Revision 1.3  1993/04/05  04:32:19  ahd
  22.  *     Add timestamp, size to information returned by directory searches
  23.  *
  24.  *     Revision 1.2  1993/04/04  19:35:14  ahd
  25.  *     Include time_t timestamp
  26.  *
  27.  */
  28.  
  29. /*--------------------------------------------------------------------*/
  30. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  31. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  32. /*           April/91                                                 */
  33. /*                                                                    */
  34. /*         Berkeley-style directory reading routine on MS-OS2         */
  35. /*--------------------------------------------------------------------*/
  36.  
  37. /*--------------------------------------------------------------------*/
  38. /*                        System include files                        */
  39. /*--------------------------------------------------------------------*/
  40.  
  41. #include <stdio.h>
  42. #include <ctype.h>
  43. #include <string.h>
  44. #include <stdlib.h>
  45. #include <assert.h>
  46. #include <time.h>
  47.  
  48. /*--------------------------------------------------------------------*/
  49. /*                         OS/2 include files                         */
  50. /*--------------------------------------------------------------------*/
  51.  
  52. #define INCL_BASE
  53. #include <os2.h>
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                    UUPC/extended include files                     */
  57. /*--------------------------------------------------------------------*/
  58.  
  59. #define FAMILY_API
  60.  
  61. #include "lib.h"
  62. #include "uundir.h"
  63. #include "dos2unix.h"
  64.  
  65. static HDIR dir_handle;
  66. static char *pathname = NULL;
  67. static struct _FILEFINDBUF findbuf;
  68.  
  69. currentfile();
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*    o p e n d i r                                                   */
  73. /*                                                                    */
  74. /*    Open a directory                                                */
  75. /*--------------------------------------------------------------------*/
  76.  
  77. extern DIR *opendirx( const char *dirname, char *pattern)
  78. {
  79.  
  80.    DIR *dirp;
  81.    USHORT rc;
  82.    USHORT count = 1;
  83.  
  84.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  85.    strcpy(pathname, dirname);
  86.  
  87.    if ((*pattern != '/') && (dirname[ strlen(dirname) - 1] != '/'))
  88.       strcat(pathname,"/");
  89.    strcat(pathname,pattern);
  90.    printmsg(5,"opendir: Opening directory %s", pathname );
  91.  
  92. /*--------------------------------------------------------------------*/
  93. /*                Read the first file in the directory                */
  94. /*--------------------------------------------------------------------*/
  95.  
  96.    dir_handle = HDIR_CREATE;
  97.    rc = DosFindFirst( pathname,
  98.             &dir_handle,
  99.             FILE_NORMAL,
  100.             &findbuf,
  101.             sizeof( findbuf ),
  102.             &count,
  103.             0L );
  104.  
  105. /*--------------------------------------------------------------------*/
  106. /*            Process the return code from the first file             */
  107. /*--------------------------------------------------------------------*/
  108.  
  109.    if ( rc == 0 )
  110.    {
  111.       dirp = malloc( sizeof( DIR ));
  112.       dirp->dirfirst = 1;
  113.       strcpy(dirp->dirid, "DIR");
  114.       return dirp;
  115.    }
  116.    else {
  117.       if (( rc != ERROR_NO_MORE_FILES ) &&
  118.           ( rc != ERROR_PATH_NOT_FOUND))
  119.          printmsg(4,"opendir: Error %d on directory %s",
  120.                   (int) rc, pathname );
  121.       return NULL;
  122.    } /* else */
  123.  
  124. } /*opendir*/
  125.  
  126. /*--------------------------------------------------------------------*/
  127. /*    r e a d d i r                                                   */
  128. /*                                                                    */
  129. /*    Get next entry in a directory                                   */
  130. /*--------------------------------------------------------------------*/
  131.  
  132. struct direct *readdir(DIR *dirp)
  133. {
  134.    USHORT rc = 0;
  135.    USHORT count = 1;
  136.  
  137.    if ( ! equal(dirp->dirid, "DIR" ))
  138.    {
  139.       printmsg(0,"readdir: No directory open to read");
  140.       panic();
  141.    }
  142.  
  143.    if (dirp->dirfirst)
  144.    {
  145.       printmsg(5,"readdir: Opening directory %s", pathname );
  146.       dirp->dirfirst = 0;
  147.    }
  148.    else
  149.       rc = DosFindNext( dir_handle,
  150.                &findbuf,
  151.                sizeof( findbuf ) ,
  152.                &count );
  153.  
  154.    if ( rc == 0 )
  155.    {
  156.       dirp->dirent.d_ino = -1;   /* no inode information */
  157.       strlwr(strcpy(dirp->dirent.d_name, findbuf.achName ));
  158.       dirp->dirent.d_namlen = findbuf.cchName;
  159.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  160.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  161.       dirp->dirent.d_modified = dos2unix( findbuf.fdateLastWrite,
  162.                                          findbuf.ftimeLastWrite );
  163.       dirp->dirent.d_size     = findbuf.cbFile;
  164.  
  165.       printmsg(4,"readdir: Returning \"%s\"", dirp->dirent.d_name);
  166.       return &(dirp->dirent);
  167.    }
  168.    else {
  169.       if ( rc != ERROR_NO_MORE_FILES )
  170.          printmsg(0,"readdir: Error %d on directory %s",
  171.                   (int) rc, pathname );
  172.       return NULL;
  173.    } /* else */
  174.  
  175. } /*readdir*/
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*    c l o s e d i r                                                 */
  179. /*                                                                    */
  180. /*    Close a directory                                               */
  181. /*--------------------------------------------------------------------*/
  182.  
  183. void closedir(DIR *dirp)
  184. {
  185.    USHORT rc;
  186.  
  187.    if ( ! equal(dirp->dirid, "DIR" ))
  188.    {
  189.       printmsg(0,"closedir: No directory open");
  190.       panic();
  191.    }
  192.  
  193.    printmsg(5,"closedir: Closing directory %s", pathname );
  194.    rc = DosFindClose( dir_handle );
  195.    if ( rc != 0 )
  196.      printmsg(0,"closedir: Error %d on directory %s",
  197.               (int) rc, pathname );
  198.    free( dirp );
  199.    dirp = NULL;
  200.    free( pathname );
  201.    pathname = NULL;
  202.  
  203. } /*closedir*/
  204.